
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jaeger
 */
public class Confirmation extends javax.swing.JFrame {

    /**
     * Creates new form Confirmation
     */
    public Confirmation() {
        initComponents();
    }
    National home;
    String user = "";
     public Confirmation(National home) {
        this.home = home;
        user = home.user; 
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        lblcouncilor = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblcongressman = new javax.swing.JLabel();
        lblvicemayor = new javax.swing.JLabel();
        lblmayor = new javax.swing.JLabel();
        lblvicegov = new javax.swing.JLabel();
        lblgovernor = new javax.swing.JLabel();
        lblvicepresident = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblsenators = new javax.swing.JLabel();
        lblpresident = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        lblcouncilor.setFont(new java.awt.Font("Georgia", 1, 12)); // NOI18N
        lblcouncilor.setText("jLabel4");

        jLabel3.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        jLabel3.setText("Councilors:");

        lblcongressman.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblcongressman.setText("Congressman:");

        lblvicemayor.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblvicemayor.setText("Vice Mayor:");

        lblmayor.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblmayor.setText("Mayor:");

        lblvicegov.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblvicegov.setText("Vice-Gov:");

        lblgovernor.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblgovernor.setText("Governor:");

        lblvicepresident.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblvicepresident.setText("Vice President:");

        jLabel4.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        jLabel4.setText("Senators:");

        lblsenators.setFont(new java.awt.Font("Georgia", 1, 14)); // NOI18N
        lblsenators.setText("jLabel4");

        lblpresident.setFont(new java.awt.Font("Georgia", 1, 18)); // NOI18N
        lblpresident.setText("President:");

        jButton2.setText("Back");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Confirm");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblpresident)
                    .addComponent(lblvicepresident)
                    .addComponent(jLabel4)
                    .addComponent(lblsenators, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblgovernor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblvicegov, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblcongressman, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblmayor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblvicemayor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblcouncilor, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblpresident)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblvicepresident)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblsenators, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton3))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblgovernor, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblvicegov)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblcongressman, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblmayor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblvicemayor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblcouncilor, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                        .addGap(21, 21, 21))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        home.submitVote();
        home.home.submitVote();
        this.setVisible(false);
        Login login = new Login();
        login.setVisible(true);
        //getReport();
            
    }//GEN-LAST:event_jButton3ActionPerformed
    void getReport()
    {
     HashMap map = new HashMap();
        Connection con = null;
        boolean result = false;
        String url1 = "jdbc:mysql://localhost:3306/";
        String db = "dbvoting";
        String driver = "com.mysql.jdbc.Driver";
        try{
            Class.forName(driver);
            con = (Connection) DriverManager.getConnection(url1+db,"root","password");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        String path = "";
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource("");
        try {
            File file = new File(url.toURI());
            path = file.getAbsolutePath()+"/newReport.jasper";
            System.out.println(file.getAbsolutePath()+"");
        } catch (URISyntaxException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
        String[] month = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        int index = Utility.getSoleInteger("select month from tblvoter where username ='"+user+"'","month");
        String realMonth = month[index];
        int day = Utility.getSoleInteger("select * from tblvoter where username ='"+user+"'","day") + 1;
        int year = Utility.getSoleInteger("select * from tblvoter where username ='"+user+"'","year") + 1920;
        String brgy = Utility.getStrings("select * from tblvoter where username ='"+user+"'", "brgy")[0];
        //
        String w = lblpresident.getText() + "\n";
        w+=lblvicepresident.getText()+"\n";
        w+="Senators: \n";
        w+=lblsenators.getText();
        w+="\n";
        w+=lblgovernor.getText();
        w+="\n";
        w+=lblvicegov.getText();
        w+="\n";
        w+=lblcongressman.getText();
        w+="\n";
        w+=lblmayor.getText();
        w+="\n";
        w+=lblvicemayor.getText();
        w+="\n";
        w+="Councilors: \n";
        w+=lblcouncilor.getText();
        
        
        //
        map.put("username",user);
        map.put("brgy", brgy);
        map.put("birthday",realMonth + " " + day +  " "  + year);
        map.put("voteInfo", w);
         JasperReport jasp;
        try {
            jasp = (JasperReport)JRLoader.loadObject(new File(path));
            JasperPrint jp = JasperFillManager.fillReport(jasp, map,con); 
            JasperViewer.viewReport(jp,false);
        } catch (JRException ex) {
            Logger.getLogger(Confirmation.class.getName()).log(Level.SEVERE, null, ex);
        }
            }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        home.setVisible(true);
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        getMyVotes();
    }//GEN-LAST:event_formWindowOpened
    void getMyVotes()
    {
        lblpresident.setText("President: " + (String)home.cbopres.getSelectedItem());
        lblvicepresident.setText("Vice President: " + (String)home.cbovice.getSelectedItem());
        String s = "<html>";
        for (JCheckBox c:home.checks)
        {
            if (c.isSelected())
                s+=c.getText()+"<br>";
        }
        s+="</html>";
        lblsenators.setText(s);
        lblgovernor.setText("Governor: " + (String)home.home.cboGovernor.getSelectedItem());
        lblvicegov.setText("Vice Governor: " + (String)home.home.cboViceGovernor.getSelectedItem());
        lblcongressman.setText("Congressman: " + (String)home.home.cboCongressman.getSelectedItem());
        lblmayor.setText("Mayor: " + (String)home.home.cboMayor.getSelectedItem());
        lblvicemayor.setText("Vice Mayor: " + (String)home.home.cboViceMayor.getSelectedItem());
        lblvicemayor.setText("Vice Mayor: " + (String)home.home.cboViceMayor.getSelectedItem());
        s = "<html>";
        for (JCheckBox c:home.home.checks)
        {
            if (c.isSelected())
                s+=c.getText()+"<br>";
        }
        s+="</html>";
        lblcouncilor.setText(s);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Confirmation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Confirmation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Confirmation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Confirmation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Confirmation().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblcongressman;
    private javax.swing.JLabel lblcouncilor;
    private javax.swing.JLabel lblgovernor;
    private javax.swing.JLabel lblmayor;
    private javax.swing.JLabel lblpresident;
    private javax.swing.JLabel lblsenators;
    private javax.swing.JLabel lblvicegov;
    private javax.swing.JLabel lblvicemayor;
    private javax.swing.JLabel lblvicepresident;
    // End of variables declaration//GEN-END:variables
}
